package com.xuzimian.global.demo.spring.security.oauth2.resource.config;

import com.google.common.collect.Maps;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableWebMvc
@ComponentScan("com.xuzimian.global.demo.spring.security.oauth2.resource.controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * ClassLoaderTemplateResolver 对应模版路径在类路径下： templateResolver.setPrefix("templates/")
     * ServletContextTemplateResolver 对应webapp 路径下：  templateResolver.setPrefix("/WEB-INF/views/");
     * FileTemplateResolver           对应绝对路径：可以配置在系统的任意路径下
     *
     * @return
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        TemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("utf-8");

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine);
        viewResolver.setContentType("text/html");
        viewResolver.setCharacterEncoding("utf-8");
        registry.viewResolver(viewResolver);

    }


    /**
     * 配置静态资源路径，并使用响应头配置浏览器缓存静态资源。
     * 下面方法中的配置：如果请求以"/resource"开头，则会使用相对路径查找并提供
     * web应用程序根目录下的"/public" 或 类路径下 "/static"的静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/scripts/**", "/styles/**","/images/**")
                .addResourceLocations("classpath:/static/styles/", "classpath:/static/scripts/","classpath:/static/images/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        final Map<String, String> parameterMap = Maps.newHashMap();
        parameterMap.put("charset", "utf-8");
        configurer.defaultContentType(new MediaType(MediaType.APPLICATION_JSON, parameterMap));
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }

    /***
     * 必须添加 MappingJackson2HttpMessageConverter 转换器，用以支持授权码模式的json解码
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter());
    }

}
